package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.RetailSet;
import pers.cj.pdos.entity.UserHangSn;
import pers.cj.pdos.mapper.RetailSetMapper;
import pers.cj.pdos.mapper.UserHangGoodsMapper;
import pers.cj.pdos.mapper.UserHangMapper;
import pers.cj.pdos.mapper.UserHangSnMapper;
import pers.cj.pdos.service.IRetailSetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.RetailGoodsVo;
import pers.cj.pdos.vo.RetailSetVo;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class RetailSetServiceImpl extends ServiceImpl<RetailSetMapper, RetailSet> implements IRetailSetService {
    @Autowired
    RetailSetMapper retailSetMapper;
    @Autowired
    UserHangMapper userHangMapper;
    @Autowired
    UserHangGoodsMapper userHangGoodsMapper;
    @Autowired
    UserHangSnMapper userHangSnMapper;

    @Override
    public RetailSetVo getVoByStore(String storeId) {
        List<RetailSetVo> list = retailSetMapper.getVoByStore(storeId);
        if (Utils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean checkStore(String companyId, String storeId) {
        return retailSetMapper.selectCount(new QueryWrapper<RetailSet>().eq("company_id",companyId).eq("store_id",storeId)) > 0;
    }

    @Override
    public List<RetailSetVo> listVo(Integer page, Integer limit, String companyId) {
        return retailSetMapper.listVo(Utils.genPage(page,limit),limit,companyId);
    }

    @Override
    public long count(String companyId) {
        return retailSetMapper.count(companyId);
    }

    @Override
    public List<RetailGoodsVo> listRetailGoodsVo(Integer page, Integer limit, String companyId, String houseId, String search) {
        return retailSetMapper.listRetailGoodsVo(Utils.genPage(page,limit),limit,companyId,houseId,search);
    }

    @Override
    public Long countRetailGoods(String companyId, String houseId, String search) {
        return retailSetMapper.countRetailGoods(companyId,houseId,search);
    }

    @Override
    public void update(RetailSet retailSet) {
        RetailSet old = retailSetMapper.selectById(retailSet.getId());
        if (!old.getHouseId().equals(retailSet.getHouseId()) || !old.getStoreId().equals(retailSet.getStoreId())){
            //如果更换了门店或者仓库，则需要清空所有的挂单
            userHangSnMapper.deleteByStoreId(retailSet.getStoreId());
            userHangGoodsMapper.deleteByStoreId(retailSet.getStoreId());
            userHangMapper.deleteByStoreId(retailSet.getStoreId());
        }

        retailSetMapper.updateById(retailSet);
    }

    @Override
    public void remove(RetailSet retailSet) {
        RetailSet old = retailSetMapper.selectById(retailSet.getId());
        userHangSnMapper.deleteByStoreId(old.getStoreId());
        userHangGoodsMapper.deleteByStoreId(old.getStoreId());
        userHangMapper.deleteByStoreId(old.getStoreId());

        retailSetMapper.deleteById(retailSet.getId());
    }

}
